package com.demo.service.impl;


import com.demo.dto.CourseDTO;
import com.demo.dto.CoursePageQueryDTO;
import com.demo.mapper.CourseMapper;
import com.demo.pojo.Course;
import com.demo.result.PageResult;
import com.demo.service.CourseService;
import com.demo.vo.CourseVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    CourseMapper courseMapper;

    @Override
    public void save(CourseDTO courseDTO) {
        Course course = new Course();
        BeanUtils.copyProperties(courseDTO, course);
        courseMapper.addCourse(course);
    }

    @Override
    public void delete(String[] courseIDs) {
        for (String courseID : courseIDs) {
            courseMapper.delete(courseID);
        }

    }

    @Override
    public void deleteById(String courseID) {
        courseMapper.delete(courseID);
    }

    @Override
    public void update(CourseDTO courseDTO) {
        courseMapper.update(courseDTO);
    }

    @Override
    public PageResult pageQuery(CoursePageQueryDTO coursePageQueryDTO) {
        System.out.println(coursePageQueryDTO);
        PageHelper.startPage(coursePageQueryDTO.getPage(), coursePageQueryDTO.getPageSize());
        Page<CourseVO> page = courseMapper.pageQuery(coursePageQueryDTO);
        System.out.println(page);
        long total = page.getTotal();
        List<CourseVO> records = page.getResult();
        return new PageResult(total, records);
    }
}
